package project.messagestack;

import java.io.InputStream;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MessageCursorAdapter extends SimpleCursorAdapter {

  Context mContext;
  
  public MessageCursorAdapter(Context context, int layout, Cursor c,
      String[] from, int[] to) {
    super(context, layout, c, from, to);
    mContext = context;
  }
  
  public static Bitmap loadContactPhoto(ContentResolver cr, Resources res, long  id) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
    if (input == null) {
        return BitmapFactory.decodeResource(res, R.drawable.ic_contact_picture);
    }
    return BitmapFactory.decodeStream(input);
}
  
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
    super.bindView(view, context, cursor);
    
    ImageView iv = (ImageView) view.findViewById(R.id.ListUserPhoto);
    TextView userNick = (TextView) view.findViewById(R.id.ListUserName);
    TextView msgContent = (TextView) view.findViewById(R.id.ListMessageContent);
    TextView msgTmp = (TextView) view.findViewById(R.id.tempTV);
    TextView msgTmp2 = (TextView) view.findViewById(R.id.tempTV2);
    TextView msgService = (TextView) view.findViewById(R.id.ListService);
    ImageView image = (ImageView)view.findViewById(R.id.ListSrvIcon);
    
    userNick.setText(cursor.getString(cursor.getColumnIndexOrThrow("address")));
    msgTmp2.setText(cursor.getString(cursor.getColumnIndexOrThrow("address")));
    //long id = cursor.getLong(cursor.getColumnIndex("person"));
    if((cursor.getString(cursor.getColumnIndexOrThrow("body"))).length()>=50){
    	msgContent.setText(((cursor.getString(cursor.getColumnIndexOrThrow("body"))).substring(0, 50)).concat("(...)"));
    	msgTmp.setText(cursor.getString(cursor.getColumnIndexOrThrow("body")));
    }
    else{
    	msgContent.setText(cursor.getString(cursor.getColumnIndexOrThrow("body")));
    	msgTmp.setText(cursor.getString(cursor.getColumnIndexOrThrow("body")));
    }   
    msgService.setText(cursor.getString(cursor.getColumnIndexOrThrow("service")));
    if((cursor.getString(cursor.getColumnIndexOrThrow("service"))).compareToIgnoreCase("buzz")== 0){
    	image.setImageResource(R.drawable.icon_buzz);
    }
    else if((cursor.getString(cursor.getColumnIndexOrThrow("service"))).compareToIgnoreCase("facebook")== 0){
    	image.setImageResource(R.drawable.icon_fb);
    }
    else if((cursor.getString(cursor.getColumnIndexOrThrow("service"))).compareToIgnoreCase("sms")== 0){
    	image.setImageResource(R.drawable.icon_sms);
    }
    else if((cursor.getString(cursor.getColumnIndexOrThrow("service"))).compareToIgnoreCase("nk")== 0){
    	image.setImageResource(R.drawable.icon_nk);
    }
    else if((cursor.getString(cursor.getColumnIndexOrThrow("service"))).compareToIgnoreCase("twitter")== 0){
    	image.setImageResource(R.drawable.icon_twitter);
    }
    else{
    	image.setImageResource(R.drawable.icon_mail);
    }
    
    long id = -1;
    if (cursor.getString(cursor.getColumnIndexOrThrow("service")).equals("SMS")) {
      String phoneNumber = cursor.getString(cursor.getColumnIndexOrThrow("address"));
    	Cursor c =mContext.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
    	                                                         Uri.encode(phoneNumber)),
    	                                    new String[] { PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},
    	                                    null,
    	                                    null,
    	                                    null);
    	if (c.moveToFirst()) {
        id = c.getLong(c.getColumnIndexOrThrow(PhoneLookup._ID));
        String s = c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
        userNick.setText(s);
        Bitmap bmp = loadContactPhoto(mContext.getContentResolver(), mContext.getResources(), id);
        bmp = Bitmap.createScaledBitmap(bmp, 100, 100, false);
        iv.setImageBitmap(bmp);
    	} else {
    	  iv.setImageResource(R.drawable.ic_contact_picture);
    	}
    } else {
      iv.setImageResource(R.drawable.ic_contact_picture);
    }
  }

}
